package tree;

import java.awt.Rectangle;

import board.Board;
import ui.EditController;
import ui.GobanPanel;
import ui.Main;
import ui.TextEditorPane;

/**
 * Display strategy for static diagrams.
 * @author TKington
 *
 */
public class DiagramDisplayStrategy implements IDisplayStrategy {
	/** Version for serialization. */
	private static final long serialVersionUID = -8870819498330424137L;
	
	/** The text for this diagram. */
	private String text;
	
	/**
	 * Creates a new DiagramDisplayStrategy.
	 * @param text the text describing the diagram
	 */
	public DiagramDisplayStrategy(String text) {
		this.text = text;
	}

	public void display(PageNode curPage, GobanPanel goban, Board board) {
		//	This should be first, so the validate event is queued before
		//	repaints caused later - this avoids jumping when 
		//	widgetPanel appears/disappears.
		Main.frame.hideWidgetPanel();
		Main.frame.enableActions(true, false);

		EditController editController = EditController.instance();
		
		if(Main.frame.editMode) {
			goban.setGobanListener(editController);
		}
		else {
			goban.setGobanListener(null);
		}
		
    	editController.setBoard(board);
    	
    	//	TODO: Set right size for this page
    	goban.setSize(new Rectangle(0, 0, 19, 19), null);
    	
    	TextEditorPane editPane = TextEditorPane.instance();
    	editPane.setText(text);
    	editPane.setCaretPosition(0);    	
	}
	
	public void setTextPaneMode(int mode) {
		TextEditorPane editPane = TextEditorPane.instance();
		if(editPane.getMode() == TextEditorPane.TEXTMODE_EDITHTML)
			text = editPane.getText();
		
		editPane.setMode(mode);

    	editPane.setText(text);
    	editPane.setCaretPosition(0);
	}
	
	public void leavingPage() {
		TextEditorPane editPane = TextEditorPane.instance();
		if(editPane.getMode() == TextEditorPane.TEXTMODE_EDITHTML)
			text = editPane.getText();
	}

	@Override
	public DiagramDisplayStrategy clone() {
		return new DiagramDisplayStrategy(text);
	}
	
	@Override
	public boolean equals(Object o) {
		if(!(o instanceof DiagramDisplayStrategy))
			return false;
		return true;
	}

	/**
	 * Returns the text for this diagram.
	 * @return the text for this diagram
	 */
	public String getText() { return text; }
	
	/**
	 * Sets the text for this diagram.
	 * @param text the text
	 */
	public void setText(String text) { this.text = text; }
}
